package com.sean.animation;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;

import com.sean.activity.R;

public class DemonView extends View {

    InputStream inputSteam = this.getContext().getResources()
            .openRawResource(R.drawable.icon);
    BitmapDrawable bmpDraw = new BitmapDrawable(inputSteam);

    float startX, startY, aimX, aimY;
    int msgTp = 1;

    public DemonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        new T1().execute(new Object());
    }

    Animation mCurrentAnimation = null;
    Transformation mTransformation = new Transformation();

    Handler myHandler = new Handler() {
        public synchronized void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                initAndStartAmination(0, 0, 200, 200, bmpDraw); //ok
                ;
                break;
            case 2:
                initAndStartAmination(100.0f, 100.0f, 0, 0, bmpDraw); //这里就会有问题，实际没有移动到(0,0)
            }
            super.handleMessage(msg);
        }
    };

    public synchronized void onDraw(Canvas canvas) {
        // this.initalAmination(int startX,int start Y)
        this.makeBallMove(canvas, this.bmpDraw);
    }

    public void initAndStartAmination(float startX, float startY, float aimX,
            float aimY, BitmapDrawable bmpDraw) {
        Animation anim = new TranslateAnimation(startX, aimX, startY
                + bmpDraw.getBitmap().getHeight(), aimY
                + bmpDraw.getBitmap().getHeight());

        this.startX = startX;
        this.startY = startY;
        this.aimX = aimX;
        this.aimY = aimY;
        this.mTransformation = new Transformation();

        anim.setDuration(1000); // 1s
        anim.setInterpolator(new AccelerateInterpolator(1.0f));
        anim.setFillAfter(true);
        startAnimation(anim);
    }

    public void startAnimation(Animation animation) {
        // animation.setStartTime(animation.);
        setAnimation(animation);
        invalidate();
    }

    public void setAnimation(Animation animation) {
        mCurrentAnimation = animation;
        if (animation != null) {
            animation.reset();
        }
    }

    public void makeBallMove(Canvas canvas, BitmapDrawable bmpDraw) {

        long curTime = SystemClock.uptimeMillis();
        if (mCurrentAnimation == null) {

            canvas.drawBitmap(bmpDraw.getBitmap(), 0, 0, null);
        } else {
            if (!mCurrentAnimation.isInitialized()) // initialize animation

                mCurrentAnimation.initialize(0, 0, 0, 0);

            boolean more = mCurrentAnimation.getTransformation(curTime,
                    mTransformation);

            if (more) {

                Matrix m = canvas.getMatrix();

                canvas.setMatrix(mTransformation.getMatrix());

                canvas.drawBitmap(bmpDraw.getBitmap(), startX, startY, null);

                canvas.setMatrix(m);

                this.invalidate();

            } else {

                // canvas.drawBitmap(bmpDraw.getBitmap(), aimX, aimY, null);
                this.mCurrentAnimation = null;
                msgTp = (msgTp + 1) % 2 == 0 ? 2 : 1;

            }

        }

    }

    private class T1 extends AsyncTask {
        Object lock = new Object();

        @Override
        protected Object doInBackground(Object... arg0) {
            while (true) {
                try {
                    Thread.sleep(1999);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (mCurrentAnimation == null) {
                    messageSend(msgTp);
                }

            }

        }

        protected void onPostExecute(Object obj) {
            // do Nothing
        }
    }

    /**
     * notify UI Thread to refresh the ChessBoard
     * 
     * @param i
     */
    private synchronized void messageSend(int i) {
        Message message = new Message();
        message.what = i;
        myHandler.sendMessage(message);
    }

}
